/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsclient;

/**
 *
 * @author dvtrung
 */
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
/**
 *
 * @author dvtrung
 */
public class JProcess {
    public static ArrayList<NetworkInterface> listIPNets() throws SocketException
    {
            ArrayList<NetworkInterface> arrIPNet = new ArrayList<NetworkInterface>();
            Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
           for (NetworkInterface netint : Collections.list(nets))
           {
               arrIPNet.add(netint);
                displayInterfaceInformation(netint);
           }
            return arrIPNet;

    }
public static String[] listNICName(ArrayList<NetworkInterface> netsint)
    {
    String []p = new String[netsint.size()];
    //ArrayList<String> listnet = new ArrayList<String>();
    for (int i = 0; i < netsint.size(); i++)
    {
        p[i]=(netsint.get(i).getDisplayName());
    }
    return p;
}

    public static String getIP(ArrayList<NetworkInterface> nets, String NICName)
    {
        for (int i = 0; i < nets.size(); i++)
        {

            NetworkInterface netint = nets.get(i);
            if(netint.getDisplayName().equals(NICName))
            {
                 Enumeration<InetAddress> inetAddresses=netint.getInetAddresses();
                 for (InetAddress inetAddress : Collections.list(inetAddresses))
                 {
                    String o = "";
                    o = o.format("%s", inetAddress);
                    if(o.contains("."))
                    {
                        return o.substring(1);
                    }
                 }
            }

        }
        return "invalid NIC";
    }
    public static String getHostName()
    {
        String res = runShellCommand("hostname");
        if(res == null) return null;
//        if(!res.endsWith("."))
//        {
//            res = res + ".";
//        }
        return res;
    }
    public static String getCurrentIP(String CardName)
    {
        String res = "";
        res = runShellCommand("ifconfig " + CardName);
        String []pack = res.split("\n");
        if(pack.length > 2)
        {
            String []ls = pack[1].split(" ");
            for(int i = 0; i < ls.length;i++)
            {
                if(ls[i].startsWith("addr:"))
                {
                    return ls[i].substring(5);
                }
            }
        }
       return res;
    }
    public static String runShellCommand(String Command)
    {
        String res = "";
        String lsString = null;
        Process process = null;
        try
        {
            process = Runtime.getRuntime().exec(Command);
            BufferedReader bufferedReader = new
            BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((lsString = bufferedReader.readLine()) != null)
            {
                System.out.println(lsString);
                res+=lsString +"\n";
            }
            try
            {
                process.waitFor();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return res;
    }
        static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
            out.printf("Display name: %s\n", netint.getDisplayName());
            out.printf("Name: %s\n", netint.getName());
            Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
            for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                out.printf("InetAddress: %s\n", inetAddress);
            }
            out.printf("\n");
         }

}
